package com.controller;

import com.pojo.User;
import com.service.UserService;
import com.service.serviceImp.UserServiceImp;
import com.sun.deploy.net.HttpResponse;
import com.utile.MyToken;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.ParamAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.PublicKey;
import java.util.List;
import java.util.concurrent.RecursiveTask;


@RestController
public class UserController {
    MyToken myToken=new MyToken();
    @Autowired
    public UserService userService;
    @RequestMapping("/getAllUser")
    public List<User> getAllUser(HttpServletResponse response, HttpServletRequest request){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("token");
        String[] all = myToken.getNew(token);

            System.out.println("=====version");
            List<User> users = userService.selAllUser();
            return users;
    }
//登录
    @RequestMapping("/login")
    public boolean login(String uname, String pas, HttpSession httpSession, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        List<User> users = userService.selAllUser();
        for (User user : users) {
            if (user.getUsername().equals(uname)&&user.getPassword().equals(pas)){
                String token = myToken.getToken(user.getUsername(), user.getId(),user.getVersion());
                response.setHeader("token",token);
                System.out.println("token========"+token);
                return true;
            }
        }
        return false;
    }

}
